const fs = require('fs')
const qrcode = require('qrcode')

const opts = {
  errorCorrectionLevel: 'H',
  type: 'image/jpeg',
  rendererOpts: {
    quality: 0.3
  }
}

function createQrcode(data) {
  return new Promise((resolve, reject) => {
    qrcode.toDataURL(data, opts, function(err, url) {
      if (err) reject(err)
      resolve(url)
    })
  })
}

function renderFile(object, cb) {
  // 遍历输出字符
  let data = ''
  object.forEach((company) => {
    createQrcode(company.info).then((url) => {
      company.info = url
      data += `<section class="editor">
  <section style="padding: 10px; text-align: center; border: 1px solid rgb(223, 223, 223);"
           class="135brush">
    <section
      style="padding-top: 10px; padding-bottom: 10px; width: 100%; margin-right: auto; margin-left: auto;"
      data-width="100%">
      <p>
        <strong style="font-size: 18px; line-height: inherit;">${company.name}</strong>
      </p>
      <p style="text-align: left;">
        <span style="font-size: 14px; line-height: normal;">${company.date}</span>
      </p>
      <p style="text-align: left;">
        <span
          style="line-height: normal; font-size: 14px;">地点：${company.address}</span><span
        style="font-size: 18px; line-height: normal;"> </span>
      </p>
      <p style="text-align: left;">
        <span style="color: #BFBFBF; font-size: 12px;">招聘专业：${company.zhuanye.join(', ')}</span>
      </p>
      <p>
        <span
          style="font-size: 14px; line-height: 22px; white-space: pre-wrap; text-align: center;"> &nbsp; &nbsp; &nbsp;<img
          src="${company.info}" alt="blob.png"/></span>
      </p>
      <p style="text-align: center;">
        <span
          style="font-size: 12px; line-height: 22px; white-space: pre-wrap; text-align: center;">扫二维码了解详情</span>
      </p>
    </section>
  </section>
</section>
<p>
  <br/>
</p>`
    })
  })
  setTimeout(() => {
    cb.header('Content-Type', 'text/html;charset=utf-8')
    return cb.end(data)
  }, 2500)
}

module.exports = renderFile
